
import axios from '~/axios'
import { joinPramas } from './manager'


// 审批流模板--------------------------

/**
 * 流程节点校验
 * @param {} rule 
 * @param {*} nodes 
 * @param {*} callback 
 * @returns 
 */
export const checkApprovalNode = (rule, nodes, callback) => {
  if (!nodes || nodes.length == 0) {
    callback(new Error('请添加流程节点'));
  } else {
      //遍历检查三个字段, approverIds: [], maxApprovalTime: '', approvalType: ''
      for(let i=0;i<nodes.length;i++){
        if(nodes[i].approverIds.length == null || nodes[i].approverIds.length == 0){
          return callback(new Error("节点【"+(i+1)+"】审核人不能为空"));
        }
        if(nodes[i].maxApprovalTime == null || nodes[i].maxApprovalTime == ''){
          return callback(new Error("节点【"+(i+1)+"】最大审核时间不能为空"));
        }
        if(nodes[i].approvalType == null || nodes[i].approvalType == ''){
          return callback(new Error("节点【"+(i+1)+"】审核类型不能为空"));
        }
      }
      callback();
  }
};


/**
 * 获取审批流模板
 * @param {*} paramIo 
 */
export function getApprovalTemplateList() {
    return axios({
      url: '/api/backend/approval/template/all',
      method: 'get'
    })
}
  
/**
 * 添加审批流模板
 * @param {*} paramIo 
 */
export function addApprovalTemplate(paramIo) {
  return axios({
    url: '/api/backend/approval/template/create',
    method: 'post',
    data: paramIo
  })
}


/**
 * 获取某个审批模板
 * @param {*} paramIo 
 */
export function getTemplateById(paramIo) {
  let url = joinPramas('/api/backend/approval/template/getById', paramIo)
  return axios({
    url: url,
    method: 'get'
  })
}

/**
 * 修改审批流模板
 * @param {*} paramIo 
 */
export function updateApprovalTemplate(templateId,paramIo) {
  //主键ID
  paramIo.templateId = templateId
  return axios({
    url: '/api/backend/approval/template/update',
    method: 'post',
    data: paramIo
  })
}

/**
 * 审批流模板上架|下架
 * @param {*} paramIo 
 */
export function approvalTemplateChangeState(templateId,state) {
  return axios({
    url: '/api/backend/approval/template/changeState/'+templateId+'/'+state,
    method: 'post'
  })
}

/**
 * 删除审批流模板
 * @param {*} paramIo 
 */
export function deleteApprovalTemplate(paramIo) {
  let url = joinPramas('/api/backend/approval/template/delete', paramIo)
  return axios({
    url: url,
    method: 'get'
  })
}

// 审批流模板--------------------------

// 审批流节点--------------------------

/**
 * 根据模板ID查询节点list
 */
export function getApprovalNodesByTid(templateId) {
  return axios({
    url: '/api/backend/approval/template/getById?templateId='+templateId,
    method: 'get'
  })
}

/**
 * 添加审批流节点
 * @param {*} paramIo 
 */
export function addApprovalNode(paramIo) {
  //数组转字符串
  paramIo.approverIds = paramIo.approverIds.join(',')
  return axios({
    url: '/api/backend/approval/node/create',
    method: 'post',
    data: paramIo
  })
}

/**
 * 修改审批流节点
 * @param {*} paramIo 
 */
export function updateApprovalNode(nodeId,paramIo) {
  //主键ID
  paramIo.nodeId = nodeId
  //数组转字符串
  paramIo.approverIds = paramIo.approverIds.join(',')
  return axios({
    url: '/api/backend/approval/node/update',
    method: 'post',
    data: paramIo
  })
}

/**
 * 删除审批流节点
 * @param {*} paramIo 
 */
export function deleteApprovalNode(paramIo) {
  let url = joinPramas('/api/backend/approval/node/delete', paramIo)
  return axios({
    url: url,
    method: 'get'
  })
}

// 审批流节点--------------------------

// 审批请求--------------------------

/**
 * 审核状态
 * @param {*} status 
 * @returns  审批状态，0：待审批，1：审批通过，2：审批拒绝，3：审批中，4：取消申请
 */
export function formatRequestStatus(status) {
  var statusName = ''
  switch (status) {
      case 0:
        statusName = '待审批'
        break
      case 1:
        statusName = '审批通过'
        break
      case 2:
        statusName = '审批拒绝'
        break
      case 3:
        statusName = '审批中'
        break
      case 4:
        statusName = '取消申请'
        break
      default:
        statusName = '-'
        break
  }
  return statusName
}

/**
 * 审批结果
 * @param {*} status 
 * @returns  审批结果，0未处理，1：同意，2：拒绝，3：处理中，4：他人已处理
 */
export function formatRecordResult(result) {
  var resultName = ''
  switch (result) {
      case 0:
        resultName = '未处理'
        break
      case 1:
        resultName = '同意'
        break
      case 2:
        resultName = '拒绝'
        break
      case 3:
        resultName = '处理中'
        break
      case 4:
        resultName = '他人已处理'
        break
      default:
        resultName = '-'
        break
  }
  return resultName
}

/**
 * 分页多条件查询审批申请
 * @param {*} paramIo 
 */
export function searchRequests(paramIo) {
  return axios({
    url: '/api/backend/approval/request/search',
    method: 'post',
    data: paramIo
  })
}

/**
 * 获取审批申请详细信息
 * @param {*} paramIo 
 */
export function getRequestInfoById(approvalRequestId) {
  return axios({
    url: '/api/backend/approval/request/getRequestInfo?approvalRequestId='+approvalRequestId,
    method: 'post'
  })
}

// 审批请求--------------------------


// 工单待办、工单已办--------------------------
export function getRecordTodo(paramIo) {
  let url = joinPramas('/api/backend/approval/request/getRecordTodo', paramIo)
  return axios({
    url: url,
    method: 'get'
  })
}

export function getRecordDone(paramIo) {
  let url = joinPramas('/api/backend/approval/request/getRecordDone', paramIo)
  return axios({
    url: url,
    method: 'get'
  })
}

export function approve(paramIo) {
  return axios({
    url: '/api/backend/approval/record/approve',
    method: 'post',
    data: paramIo
  })
}

// 工单待办、工单已办--------------------------
